﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using DataTransferObject;
using System.Data.Entity;

namespace BusinessLogicLayer
{
    public class BenhBanDau_NoiDungDieuTriBLL : BLLBase
    {
        private readonly Ibn_BenhBanDau_NoiDungDieuTriDAL _dal;

        public BenhBanDau_NoiDungDieuTriBLL(string connectionString = "")
            : base(connectionString)
        {
            this._dal = new bn_BenhBanDau_NoiDungDieuTriDAL(this.DatabaseFactory);
        }

        public IQueryable<BenhBanDau_NoiDungDieuTriDto> GetWhereBenhNhan(int facilityId, string key = "")
        {
            var query = this._dal.GetAll()
                .Where(e => e.IdBenhNhan == facilityId)
                .Select(e => new BenhBanDau_NoiDungDieuTriDto
                    {
                        Id = e.Id,
                        ChuanDoanBenh = e.ChuanDoanBenh,
                        Date = e.Date,
                        GhiChu = e.GhiChu,
                        HuongDieuTri = e.HuongDieuTri,
                        IdBacSi = e.IdBacSi,
                        IdBenhNhan = e.IdBenhNhan,
                        IdPhongDieuTri = e.IdPhongDieuTri,
                        IdThongTinBenhBanDau = e.IdThongTinBenhBanDau
                    });

            if (!string.IsNullOrEmpty(key))
            {
            }
            return query;
        }
        public IQueryable<BenhBanDau_NoiDungDieuTriDto> Get(int facilityId, string key = "")
        {
            var query = this._dal.GetAll()
                .Where(e => e.Id == facilityId)
                .Select(e => new BenhBanDau_NoiDungDieuTriDto
                {
                    Id = e.Id,
                    ChuanDoanBenh = e.ChuanDoanBenh,
                    Date = e.Date,
                    GhiChu = e.GhiChu,
                    HuongDieuTri = e.HuongDieuTri,
                    IdBacSi = e.IdBacSi,
                    IdBenhNhan = e.IdBenhNhan,
                    IdPhongDieuTri = e.IdPhongDieuTri,
                    IdThongTinBenhBanDau = e.IdThongTinBenhBanDau
                });

            /*if (!string.IsNullOrEmpty(key))
            {
                query = query.Where(e => e.Ten.StartsWith(key));
            }*/
            return query;
        }

        public IQueryable<BenhBanDau_NoiDungDieuTriDto> GetAll()
        {
            var query = this._dal.GetAll()
                .Select(e => new BenhBanDau_NoiDungDieuTriDto
                {
                    Id = e.Id,
                    ChuanDoanBenh = e.ChuanDoanBenh,
                    Date = e.Date,
                    GhiChu = e.GhiChu,
                    HuongDieuTri = e.HuongDieuTri,
                    IdBacSi = e.IdBacSi,
                    IdBenhNhan = e.IdBenhNhan,
                    IdPhongDieuTri = e.IdPhongDieuTri,
                    IdThongTinBenhBanDau = e.IdThongTinBenhBanDau
                });
            return query;
        }
        public void Update(BenhBanDau_NoiDungDieuTriDto dto)
        {
            var entity = this._dal.GetAll().FirstOrDefault(e => e.Id == dto.Id);
            if (entity == null) throw new Exception("Đối tượng không tồn tại");
            entity.HuongDieuTri = dto.HuongDieuTri;
            entity.GhiChu = dto.GhiChu;
            entity.ChuanDoanBenh = dto.ChuanDoanBenh;
            entity.Date = dto.Date;
            //entity.Ten = dto.Ten;
            this.SaveChanges();
        }
        public void Insert(BenhBanDau_NoiDungDieuTriDto dto)
        {
            var entity = new bn_BenhBanDau_NoiDungDieuTri()
            {
                ChuanDoanBenh = dto.ChuanDoanBenh,
                Date = dto.Date,
                GhiChu = dto.GhiChu,
                HuongDieuTri = dto.HuongDieuTri,
                IdBacSi = dto.IdBacSi,
                IdBenhNhan = dto.IdBenhNhan,
                IdPhongDieuTri = dto.IdPhongDieuTri,
                IdThongTinBenhBanDau = dto.IdThongTinBenhBanDau
            };
            this._dal.Add(entity);
            this.SaveChanges();
            dto.Id = entity.Id;
        }
        public void Delete(int id, int facilityId)
        {
            var entity = this._dal.GetAll().FirstOrDefault(e => e.Id == id && e.Id == facilityId);
            if (entity == null) throw new Exception("Đối tượng không tồn tại");
            this._dal.Delete(entity);
            this.SaveChanges();
        }
        public void DeleteWhereHSBenhNhan(int id)
        {
            this._dal.Delete(e => e.IdBenhNhan == id);
            this.SaveChanges();
        }
    }
}
